import { Meteor } from 'meteor/meteor';
import {Users} from './users/users.js';
import {Transactions} from './transaction/transactions.js';
import {TradeOp} from './tradehandler.js';
let faker = require('faker/locale/zh_CN');
//import {HfcOperate} from '../../server/hfc/hfcHelper.js';
import {wsRest} from '../../server/hfc/ws_hfc.js';
import {mycbops} from './blocksmgr/chainblocks4mongo.js'; 





Meteor.methods({
  'trans.create'(uidFrom,uidTo,tag,data) {
      TradeOp.applyOp(uidFrom,uidTo,tag,data);
  },
  
  //确认交易
  'trans.confirm'(fid,tid,paynumber) {
    TradeOp.confirmTrade(fid,tid,paynumber)
  },

  'trans.getprojects'(fid) {
    var query = {$and:[{"utype":"项目"},{"createdBy": fid}]};
    return Users.find(query, { sort: { createdAt: -1 } }).fetch();
  },

  'trans.getallblocks'() {
    console.log('jjjjjjjjjjjjjjjj------------');
    console.log("get blocks start .......");
                var mybs = wsRest.getBlocks();
                //console.log(mybs);
                let i = 0;
                //for(i = 0; i < mybs.length-1; i++){

                mybs.forEach((tmpblock) => {
                    let h = tmpblock.blockheight;
                    let qstr = {"blockheight":h};
                    let tmprs = null;
                    try{
                        let all = mycbops.find().fetch();
                        if(all.length >  0){
                           // console.log('find ==========='+JSON.stringify(qstr));
                            tmprs = mycbops.find(qstr).fetch();
                        }
                    }catch(e){
                        tmprs = null;
                    }
                    //console.log("rs cccccccccccccc="+JSON.stringify(tmprs));
                    if(tmprs == null || tmprs.length == 0){
                        mycbops.insert(tmpblock);
                        //console.log("insert blocks "+tmpblock);
                    }
                    //console.log("view blocks "+JSON.stringify(tmpblock));
       
    });



               /*for (var tmpblock in mybs) {   
                    //let tmpblock = mybs[i];
                    //console.log(tmpblock);
                    let h = tmpblock.blockheight;
                    let qstr = {"blockheight":h};
                    let tmprs = null;
                    try{
                        let all = mycbops.find().fetch();
                        if(all.length >  0){
                            console.log('find ==========='+JSON.stringify(qstr));
                            tmprs = mycbops.find(qstr).fetch();
                        }
                    }catch(e){
                        tmprs = null;
                    }
                    console.log("rs cccccccccccccc="+JSON.stringify(tmprs));
                    if(tmprs == null || tmprs.length == 0){
                        mycbops.insert(tmpblock);
                        console.log("insert blocks "+tmpblock);
                    }
                };*/
                console.log("get blocks end .......");
      return "ok";
  },

});
